package handler

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"post-hub/inits"
	"post-hub/internal/dto"
	"post-hub/internal/model"
	"post-hub/response"
)

func CreateLove(c *gin.Context) {

	var love model.Love
	err := c.ShouldBind(&love)
	if err != nil {
		response.Error(c, "解析失败")
		return
	}
	var dist model.Dist
	err = c.ShouldBind(&dist)
	if err != nil {
		response.Error(c, "解析失败")
		return
	}
	love = model.Love{
		UserId: love.UserId,
		DistId: love.DistId,
	}
	err = dto.CreateLove(&love)
	if err != nil {
		response.Error(c, "关注失败")
		return
	}
	response.Success(c, "关注成功", love)
	inits.DB.Begin()
	dist = model.Dist{
		Model: gorm.Model{
			ID: uint(love.DistId),
		},
		Fans: dist.Fans + 1,
	}
	dto.UpdateDist(&dist, int(dist.ID))

	inits.DB.Commit()

}
